package com.ly.ocw.service.weixin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ly.ocw.vo.wxcard.cash.CashCard;
import com.ly.ocw.vo.wxcard.cash.CashDetail;
import com.ly.ocw.vo.wxcard.cash.CreateCashDto;
import org.apache.http.client.ClientProtocolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ly.ocw.entity.CrmMember;
import com.ly.ocw.entity.weixin.WeixinCard;
import com.ly.ocw.entity.weixin.WeixinCardTestwhitelist;
import com.ly.ocw.exception.ServerException;
import com.ly.ocw.repository.weixin.WeixinCardDao;
import com.ly.ocw.rest.dto.BaseResponseDto;
import com.ly.ocw.rest.dto.card.CardUpdateDto;
import com.ly.ocw.rest.dto.card.DecodeCardCodeDto;
import com.ly.ocw.rest.dto.card.TestWhiteListRespDto;
import com.ly.ocw.service.CrmMemberService;
import com.ly.ocw.utils.HttpUtil;
import com.ly.ocw.vo.wxcard.Base_info;
import com.ly.ocw.vo.wxcard.member.Card;
import com.ly.ocw.vo.wxcard.member.CreateCardDto;
import com.ly.ocw.vo.wxcard.member.Custom_cell1;
import com.ly.ocw.vo.wxcard.member.Custom_field;
import com.ly.ocw.vo.wxcard.Date_info;
import com.ly.ocw.vo.wxcard.member.Member_card;
import com.ly.ocw.vo.wxcard.Sku;
import com.ly.ocw.vo.wxcard.response.CreateCardResponse;

/**
 * 微信会员卡服务
 * 
 * @author Peter
 *
 */
@Component
public class WeixinCardService {

	private static Logger logger = LoggerFactory.getLogger(WeixinCardService.class);
	@Autowired
	private WeixinCardDao weixinCardDao;
	@Autowired
	private WeixinConf weixinConf;

	private static ObjectMapper mapper = new ObjectMapper();
	{
		mapper.setSerializationInclusion(Include.NON_NULL);
	}

	@Autowired
	private WeixinAccessTokenService weixinAccessTokenService;

	@Autowired
	private WeixinCardTestwhitelistService weixinCardTestwhitelistService;

	@Autowired
	private CrmMemberService crmMemberService;

	public String getCardInfo(String cardId) throws ClientProtocolException, IOException, ServerException {
		String body = "{\"card_id\":\"" + cardId + "\"}";
		String url = "https://api.weixin.qq.com/card/get?access_token=" + weixinAccessTokenService.getAccessToken();
		String respStr = HttpUtil.httpPost(url, body);
		return respStr;
	}

	// card_id = pMBADj7D5ke257fPW0YL2OpZN_3A
	// card_id = pMBADj4nloPYJPIv5yC8M6YvO8Do
	public CreateCardResponse createVipCard() throws ServerException, ClientProtocolException, IOException {
		CreateCardDto dto = createCard();

		String body = mapper.writeValueAsString(dto);
		String url = "https://api.weixin.qq.com/card/create?access_token=" + weixinAccessTokenService.getAccessToken();
		String respStr = HttpUtil.httpPost(url, body);

		CreateCardResponse resp = mapper.readValue(respStr, CreateCardResponse.class);
		if (resp.getErrcode().equals("0")) {
			WeixinCard weixinCard = new WeixinCard();
			weixinCard.setCardId(resp.getCard_id());
			weixinCard.setCardJson(body);
			weixinCard.setCreateDate(new Date());
			weixinCard.setStatus("CARD_STATUS_NOT_VERIFY");
			weixinCard.setWeixinPublicId(weixinConf.getPublicId());
			weixinCard.setCardName(dto.getCard().getMember_card().getBase_info().getTitle());

			weixinCardDao.save(weixinCard);
		}

		return resp;
	}


    /**
     * 创建现金券
     * @param leastCost 起用金额（单位为元）
     * @param reduceCost 券面金额（单位为元）
     *
     * @return
     * @throws ServerException
     * @throws ClientProtocolException
     * @throws IOException
     */
    public CreateCardResponse createCashCard(int leastCost, int reduceCost)
            throws ServerException, ClientProtocolException, IOException {

		CreateCashDto dto = createCash(leastCost, reduceCost);

		String body = mapper.writeValueAsString(dto);
		String url = "https://api.weixin.qq.com/card/create?access_token=" + weixinAccessTokenService.getAccessToken();
		String respStr = HttpUtil.httpPost(url, body);

		CreateCardResponse resp = mapper.readValue(respStr, CreateCardResponse.class);
		if (resp.getErrcode().equals("0")) {
			WeixinCard weixinCard = new WeixinCard();
			weixinCard.setCardId(resp.getCard_id());
			weixinCard.setCardJson(body);
			weixinCard.setCreateDate(new Date());
            //TODO 现金券状态
			weixinCard.setStatus("CARD_STATUS_NOT_VERIFY");
			weixinCard.setWeixinPublicId(weixinConf.getPublicId());
			weixinCard.setCardName(dto.getCard().getCash().getBase_info().getTitle());

			weixinCardDao.save(weixinCard);
		}

		return resp;
	}

    private CreateCardDto createCard() {
        CreateCardDto dto = new CreateCardDto();
        Card card = new Card();
        card.setCard_type("MEMBER_CARD");
        Member_card member_card = new Member_card();
        // 手动激活会员卡回调链接
        member_card.setActivate_url("http://wxdzhy.haiziwang.com/boycard/cardcallback/activate");
        Base_info base_info = new Base_info();
        base_info.setBrand_name("孩子王");// 商户名字,字数上限为12个汉字
        base_info.setCan_give_friend(false);

        // Code展示类型，"CODE_TYPE_TEXT"，文本；"CODE_TYPE_BARCODE"，一维码 ；
        // "CODE_TYPE_QRCODE"，二维码；"CODE_TYPE_ONLY_QRCODE",二维码无code显示；"CODE_TYPE_ONLY_BARCODE",一维码无code显示；
        base_info.setCode_type("CODE_TYPE_BARCODE");
        base_info.setColor("Color080");

        Date_info date_info = new Date_info();
        date_info.setType("DATE_TYPE_PERMANENT");// DATE_TYPE_FIX_TIME_RANGE
        // 表示固定日期区间，DATE_TYPE_FIX_TERM表示固定时长（自领取后按天算），DATE_TYPE_PERMANENT
        // 表示永久有效。

        base_info.setDate_info(date_info);
        base_info.setDescription("结账时出示此电子会员卡方可享受会员权益。");
        base_info.setGet_limit(20);
        base_info.setLogo_url("https://mmbiz.qlogo.cn/mmbiz/Wzian0BjicGyzrngHa3eeuMz27FOVyrPs6yRibicOypzCL3T1VU1LAYLVtDXqy3rLicaBjaAAuXxtXgGCLWN9sWn7mQ/0");
        base_info.setNeed_push_on_view(true);
        base_info.setNotice("使用时请让店员扫码");
        base_info.setService_phone("400 828 8686");
        Sku sku = new Sku();
        sku.setQuantity("900000000");
        base_info.setSku(sku);
        base_info.setTitle("孩子王会员卡");// 会员卡名称
        base_info.setSub_title("贴心陪伴 为爱领航");// 会员卡副标题
        base_info.setUse_custom_code(true);

        // http://120.26.59.104/boycardweb/custom_url.html
        base_info.setCustom_url("http://wxdzhy.haiziwang.com/boycard/cardcallback/custom_url");
        base_info.setCustom_url_name("我的积分");
        base_info.setCustom_url_sub_title("计赚积分");

        Custom_cell1 custom_cell1 = new Custom_cell1();
        custom_cell1.setName("微购商城");
        custom_cell1.setTips("天天特卖");
        custom_cell1.setUrl("http://wxdzhy.haiziwang.com/boycard/cardcallback/custom_cell1");
        member_card.setCustom_cell1(custom_cell1);

        base_info.setPromotion_url("http://wxdzhy.haiziwang.com/boycard/cardcallback/promotion_url");
        base_info.setPromotion_url_name("孩子王APP");
        base_info.setPromotion_url_sub_title("下载孩子王APP");

        // 门店
        List<Long> location_id_list = new ArrayList<Long>();
        // 孩子王门店
        if(weixinConf.getPublicId().intValue()==3){
            location_id_list.add(285177926L);
            location_id_list.add(278806342L);
            location_id_list.add(279422493L);
            location_id_list.add(285172241L);
            location_id_list.add(278898927L);
        }else{
            // 狼牙门店
            location_id_list.add(291326266L);
            location_id_list.add(286935100L);
        }
        base_info.setLocation_id_list(location_id_list);// 285633618

        member_card.setBase_info(base_info);

        String prerogativeStr = "尊敬的孩子王会员，各个等级的会员特权不同，基础特权和各等级会员的专属特权分别如下：\n\n  基础特权：\n    1. 可免费参加孩子王所有线下亲子活动\n    2. 宝宝生日当天可享受购物双倍积分\n    3. 可享受会员价商品\n    4. 可免费为宝宝理发\n    5. 育儿顾问免费提供育儿指导服务\n\n  银卡会员特权：\n    1. 银卡会员每消费1元积1分\n    2. 银卡会员一年内消费额在2000元及以上可升级为金卡会员\n    3. 银卡会员单次消费7000元及以上可直接升级为铂金卡会员\n    4. 可享受其他不定期推出的银卡会员专享服务\n    5. 银卡会员无需保级，永久有效。\n\n  金卡会员特权：\n    1. 金卡会员每消费1元积1.1分\n    2. 金卡会员一年内消费额在5000元及以上可升级为铂金卡\n    3. 可享受其他不定期推出的金卡会员专享服务\n    4. 金卡会员一年内消费额满1800元即可保级成功\n\n  铂金卡会员特权：\n    1. 金卡会员每消费1元积1.25分\n    2. 可享受其他不定期推出的铂金卡会员专享服务\n    3. 铂金卡会员一年内消费额满3500元即可保级成功";
        member_card.setPrerogative(prerogativeStr);
        member_card.setSupply_balance(false);
        member_card.setSupply_bonus(true);

        // 会员信息类目名称。
        // FIELD_NAME_TYPE_LEVEL等级；
        // FIELD_NAME_TYPE_COUPON优惠券；
        // FIELD_NAME_TYPE_STAMP印花；
        // FIELD_NAME_TYPE_DISCOUNT折扣；
        // FIELD_NAME_TYPE_ACHIEVEMEN成就；//目前不能显示
        // FIELD_NAME_TYPE_MILEAGE里程。
        Custom_field custom_field1 = new Custom_field();
        custom_field1.setName_type("FIELD_NAME_TYPE_LEVEL");// FIELD_NAME_TYPE_LEVEL等级；
        custom_field1.setUrl("http://wxdzhy.haiziwang.com/boycard/cardcallback/custom_field1_url");
        member_card.setCustom_field1(custom_field1);

        Custom_field custom_field2 = new Custom_field();
        custom_field2.setName_type("FIELD_NAME_TYPE_COUPON");// FIELD_NAME_TYPE_COUPON优惠券；
        custom_field2.setUrl("http://wxdzhy.haiziwang.com/boycard/cardcallback/custom_field2_url");
        member_card.setCustom_field2(custom_field2);

		/*
		 * // 成就 Custom_field custom_field3 = new Custom_field();
		 * custom_field3.setName_type("FIELD_NAME_TYPE_ACHIEVEMEN");
		 * custom_field3
		 * .setUrl("http://wx.wonderlink.com.cn/boycard/card/custom_field3");
		 * member_card.setCustom_field3(custom_field3 );
		 */

        card.setMember_card(member_card);

        dto.setCard(card);

        return dto;
    }

    /**
     * 创建现金券
     * @param leastCost 起用金额（单位为元）
     * @param reduceCost 券面金额（单位为元）
     * @return
     */
	private CreateCashDto createCash(int leastCost, int reduceCost) {

        /**************************************基础信息************************************/
		Base_info base_info = new Base_info();
		base_info.setBrand_name("孩子王");// 商户名字,字数上限为12个汉字
		base_info.setCan_give_friend(false);

		// Code展示类型，"CODE_TYPE_TEXT"，文本；"CODE_TYPE_BARCODE"，一维码 ；
		// "CODE_TYPE_QRCODE"，二维码；"CODE_TYPE_ONLY_QRCODE",二维码无code显示；"CODE_TYPE_ONLY_BARCODE",一维码无code显示；
		base_info.setCode_type("CODE_TYPE_BARCODE");
		base_info.setColor("Color080");

		Date_info date_info = new Date_info();
		date_info.setType("DATE_TYPE_PERMANENT");// DATE_TYPE_FIX_TIME_RANGE
													// 表示固定日期区间，DATE_TYPE_FIX_TERM表示固定时长（自领取后按天算），DATE_TYPE_PERMANENT
													// 表示永久有效。

		base_info.setDate_info(date_info);
		base_info.setDescription("结账时请出示此券方能享受现金抵扣。");
		base_info.setGet_limit(20);
		base_info.setLogo_url("https://mmbiz.qlogo.cn/mmbiz/Wzian0BjicGyzrngHa3eeuMz27FOVyrPs6yRibicOypzCL3T1VU1LAYLVtDXqy3rLicaBjaAAuXxtXgGCLWN9sWn7mQ/0");

		base_info.setNotice("使用时请让店员扫码");
		base_info.setService_phone("400 828 8686");
		Sku sku = new Sku();
		sku.setQuantity("900000000");
		base_info.setSku(sku);
		base_info.setTitle("孩子王" + reduceCost + "元現金券");// 会员卡名称
		base_info.setSub_title("消费满" + leastCost + "元即可使用");// 会员卡副标题
		base_info.setUse_custom_code(false);

		base_info.setCustom_url("http://wxdzhy.haiziwang.com/boycard/cardcallback/custom_cell1");
		base_info.setCustom_url_name("立即使用");
		base_info.setCustom_url_sub_title("微购商城");

		base_info.setPromotion_url("http://wxdzhy.haiziwang.com/boycard/cardcallback/promotion_url");
		base_info.setPromotion_url_name("孩子王APP");
		base_info.setPromotion_url_sub_title("下载孩子王APP");
		// 门店
		List<Long> location_id_list = new ArrayList<Long>();
		// 孩子王门店
		if(weixinConf.getPublicId().intValue()==3){
			location_id_list.add(285177926L); 
			location_id_list.add(278806342L);
			location_id_list.add(279422493L); 
			location_id_list.add(285172241L);
			location_id_list.add(278898927L);
		}else{
			// 狼牙门店
			location_id_list.add(291326266L);
			location_id_list.add(286935100L);
		}
		base_info.setLocation_id_list(location_id_list);// 285633618

        /**************************************现金券详细************************************/
        CashDetail cashDetail = new CashDetail();
		cashDetail.setBase_info(base_info);
        //设置金额 （单位为分）
        cashDetail.setLeast_cost(leastCost*100);
        cashDetail.setReduce_cost(reduceCost*100);

        /**************************************现金券************************************/
        CashCard card = new CashCard();
        card.setCard_type("CASH");
        card.setCash(cashDetail);

        /**************************************包装对象************************************/
        CreateCashDto dto = new CreateCashDto();
        dto.setCard(card);
        return dto;
	}

	/**
	 * 激活会员卡
	 * 
	 * @param cardId
	 * @param code
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws ServerException
	 */
	public String activateCard(String cardId, String code, String openid) throws ClientProtocolException, IOException, ServerException {
		logger.info("激活会员卡：{" + cardId + ",code：" + code + ",openid:" + openid + "}");
		// 1. 用户绑定会员卡时已经读取CRM会员卡信息，并保存到数据库，此时从数据库读取会员信息，但是用户领取卡券的时候没有openid，在激活会员卡的时候需要更新openid
		CrmMember crmMember = crmMemberService.getByCardIdAndCode(cardId, code);
		if(crmMember == null){
			logger.info("激活会员卡失败，无会员卡信息 null");
			return "";
		}else{
			crmMember.setOpenid(openid);
			crmMemberService.update(crmMember);
			// HzwMembershipInfo hzwMembershipInfo =
			// crmMemberService.receiveMemberInfo(String mobile);
	
			String body = "{\"init_bonus\": " + crmMember.getPointsBalance() + ",\"init_balance\": " + crmMember.getPointsBalance() + ",\"membership_number\": \""
					+ crmMember.getCardNumber() + "\",\"code\": \"" + crmMember.getCardNumber() + "\",\"card_id\": \"" + cardId + "\",\"custom_field_value1\":\""+ crmMember.getLevelName() +"\"}";
			String url = "https://api.weixin.qq.com/card/membercard/activate?access_token=" + weixinAccessTokenService.getAccessToken();
			String respStr = HttpUtil.httpPost(url, body);
	
			logger.info("激活会员卡respStr：" + respStr);
			
			
			//更新会员信息，这样才能显示会员等级为“银卡”而不是显示“查看”。
			logger.info("start 调微信接口，更新会员信息 ");
			try {
				String body_1 = "{\"code\": \""+crmMember.getCardNumber()+"\",\"card_id\":\""+crmMember.getCardid()+"\",\"record_bonus\": \"获得0积分\",\"add_bonus\": 0,\"add_balance\": 0,\"record_balance\": \"无\",\"custom_field_value1\": \""+ crmMember.getLevelName() +"\",}";
				String url_1 = "https://api.weixin.qq.com/card/membercard/updateuser?access_token=" + weixinAccessTokenService.getAccessToken();
				String respStr_1 = HttpUtil.httpPost(url_1, body_1);
				CardUpdateDto resp = mapper.readValue(respStr_1, CardUpdateDto.class);

				if ("0".equals(resp.getErrcode()) && "ok".equals(resp.getErrmsg())) {
					logger.info("end 更新会员信息成功！"+crmMember);
				}else{
					logger.error("end 更新会员信息失败！"+crmMember);
				}
			} catch (Exception e) {
				logger.error("end 更新会员信息失败！",e);
			}
			
			
			
			
			return respStr;
		}
	}

	public List<WeixinCard> getCards() {
		return weixinCardDao.getAllByWeixinPublicIdOnOk(weixinConf.getPublicId());
	}

	/**
	 * 读取当前公众号的所有卡列表
	 * 
	 * @return
	 */
	public List<WeixinCard> getAllCards() {
		return weixinCardDao.getAllByWeixinPublicId(weixinConf.getPublicId());
	}

	public String deleteCard(String cardId) throws ServerException, ClientProtocolException, IOException {
		String body = "{\"card_id\": \"" + cardId + "\"}";
		String url = "https://api.weixin.qq.com/card/delete?access_token=" + weixinAccessTokenService.getAccessToken();
		String respStr = HttpUtil.httpPost(url, body);

		BaseResponseDto resp = mapper.readValue(respStr, BaseResponseDto.class);

		if ("0".equals(resp.getErrcode()) && "ok".equals(resp.getErrmsg())) {
			weixinCardDao.deleteCard(cardId);
		}

		return respStr;
	}

    /**
     * 消费卡券
     * @param code
     * @return
     * @throws ServerException
     * @throws ClientProtocolException
     * @throws IOException
     */
	public boolean comsumerCard(String code){
        boolean result = false;
        try {
            String body = "{\"code\": \"" + code + "\"}";
            String url = "https://api.weixin.qq.com/card/code/consume?access_token=" + weixinAccessTokenService.getAccessToken();
            String respStr = HttpUtil.httpPost(url, body);

            BaseResponseDto resp = mapper.readValue(respStr, BaseResponseDto.class);
            if ("0".equals(resp.getErrcode()) && "ok".equals(resp.getErrmsg())) {
                //TODO 记录消费
                result = true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
	}

	/**
	 * 添加测试白名单
	 * 
	 * @return
	 * @throws IOException
	 * @throws ClientProtocolException
	 * @throws ServerException
	 */
	// https://api.weixin.qq.com/card/testwhitelist/set?access_token=TOKEN
	public String addTestWhiteList(WeixinCardTestwhitelist weixinCardTestwhitelist) throws ClientProtocolException, IOException, ServerException {
		// https://api.weixin.qq.com/card/testwhitelist/set?access_token=TOKEN
		List<String> wxNameList = weixinCardTestwhitelist.getNameList();
		List<WeixinCardTestwhitelist> poList = new ArrayList<WeixinCardTestwhitelist>();
		String usernames = "";
		for (int i = 0; i < wxNameList.size(); i++) {
			if (i == (wxNameList.size() - 1)) {
				usernames += "\"" + wxNameList.get(i) + "\"";
			} else {
				usernames += "\"" + wxNameList.get(i) + "\",";
			}
			WeixinCardTestwhitelist po = new WeixinCardTestwhitelist();
			po.setCreateDate(new Date());
			po.setWeixinPublicId(weixinConf.getPublicId());
			po.setName(wxNameList.get(i));
			poList.add(po);
		}
		String body = "{\"username\": [" + usernames + "]}";
		String url = "https://api.weixin.qq.com/card/testwhitelist/set?access_token=" + weixinAccessTokenService.getAccessToken();
		String respStr = HttpUtil.httpPost(url, body);

		TestWhiteListRespDto resp = mapper.readValue(respStr, TestWhiteListRespDto.class);

		if ("0".equals(resp.getErrcode()) && "ok".equals(resp.getErrmsg())) {
			weixinCardTestwhitelistService.deleteAllByWeixinId(weixinConf.getPublicId());
			for (WeixinCardTestwhitelist weixinCardTestwhitelist2 : poList) {
				weixinCardTestwhitelistService.save(weixinCardTestwhitelist2);
			}

		}

		return respStr;

	}

	public void updateCardStatus(String cardId, String status) {
		weixinCardDao.updateCardStatus(cardId, status);
	}

	public static void main(String[] args) {
		String prerogativeStr = "尊敬的孩子王会员，各个等级的会员特权不同，基础特权和各等级会员的专属特权分别如下：\n\n  基础特权：\n    1.可免费参加孩子王所有线下亲子活动\n    2.宝宝生日当天可享受购物双倍积分\n    3.可享受会员价商品\n    4.可免费为宝宝理发\n    5.育儿顾问免费提供育儿指导服务\n\n  银卡会员特权：\n    1.银卡会员每消费1元积1分\n    2.银卡会员一年内消费额在2000元及以上可升级为金卡会员\n    3	银卡会员单次消费7000元及以上可直接升级为铂金卡会员\n    4可享受其他不定期推出的银卡会员专享服务\n    5银卡会员无需保级，永久有效。\n\n  金卡会员特权：\n    1.金卡会员每消费1元积1.1分\n    2.金卡会员一年内消费额在5000元及以上可升级为铂金卡\n    3.可享受其他不定期推出的金卡会员专享服务\n    4.	金卡会员一年内消费额满1800元即可保级成功\n\n  铂金卡会员特权：\n    1.金卡会员每消费1元积1.25分\n    2.可享受其他不定期推出的铂金卡会员专享服务\n    3.铂金卡会员一年内消费额满3500元即可保级成功";
		System.out.println(prerogativeStr);
	}

	// Code解码接口code解码接口支持两种场景：
	// 1.商家获取choos_card_info后，将card_id和encrypt_code字段通过解码接口，获取真实code。
	// 2.卡券内跳转外链的签名中会对code进行加密处理，通过调用解码接口获取真实code。
	public String decodeEncryptCode(String encryptCode) throws ClientProtocolException, IOException, ServerException {
		String body = "{\"encrypt_code\": [\"" + encryptCode + "\"]}";
		String url = "https://api.weixin.qq.com/card/code/decrypt?access_token=" + weixinAccessTokenService.getAccessToken();
		String respStr = HttpUtil.httpPost(url, body);
		DecodeCardCodeDto resp = mapper.readValue(respStr, DecodeCardCodeDto.class);

		String code = "";
		if ("0".equals(resp.getErrcode()) && "ok".equals(resp.getErrmsg())) {
			code = resp.getCode();
		}
		return code;
	}

	public List<WeixinCard> getAllUsedCards() {

		return weixinCardDao.getAllUsedCards();
	}

	public List<WeixinCard> getAllUsedCardsByWeixinId() {
		return weixinCardDao.getAllUsedCardsByWeixinId(weixinConf.getPublicId());
	}

	/**
	 * 调微信接口，更新会员信息
	 * @param crmMember
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public void updateWeixinMembercard(CrmMember crmMember,Double add_bonus) {
		logger.info("start 调微信接口，更新会员信息 ");
		try {
			String body = "{\"code\": \""+crmMember.getCardNumber()+"\",\"card_id\":\""+crmMember.getCardid()+"\",\"record_bonus\": \"获得"+add_bonus+"积分\",\"add_bonus\": "+ add_bonus +",\"add_balance\": 0,\"record_balance\": \"无\",\"custom_field_value1\": \""+ crmMember.getLevelName() +"\",}";
			String url = "https://api.weixin.qq.com/card/membercard/updateuser?access_token=" + weixinAccessTokenService.getAccessToken();
			String respStr = HttpUtil.httpPost(url, body);
			CardUpdateDto resp = mapper.readValue(respStr, CardUpdateDto.class);

			if ("0".equals(resp.getErrcode()) && "ok".equals(resp.getErrmsg())) {
				logger.info("end 更新会员信息成功！"+crmMember);
			}else{
				logger.error("end 更新会员信息失败！"+crmMember);
			}
		} catch (Exception e) {
			logger.error("end 更新会员信息失败！",e);
		}
		
	}
	
	


}
